<template>
  <!-- <div id="nav">
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link>
  </div> -->
  <router-view />
  <!-- <transition name="van-fade">
		<keep-alive include="case,manage,fans">
			<router-view/>
		</keep-alive>
	</transition> -->
</template>

<script>
import { myMixins } from "@/assets/js/mixins.js";

export default {
  mixins: [myMixins],
  data() {
    return {
      isFirst: true,
    };
  },
  created() {
    this.configItemShare();
    this.ConfigItem();
    console.log("=--------ConfigItem------------");
  },
  mounted() {
    const _this = this;
	// ||this.$store.state.openid
    let openid = this.$store.state.openid;
	if (process.env.NODE_ENV === 'production') {
		openid = localStorage.getItem("OPENID");
	}
    console.log("openid:"+openid);
    if (!openid || openid==null ||  openid=='null' || openid == 'undefined' || openid == undefined) {
      // this.getMerchantItem(localStorage.getItem('OPENID'))
      _this.getWeixinConfig();
    }else{
		// this.getPromoterItema(openid);
		this.getMerchantItem(openid);
    }
    this.iphoneConfig();
  },
  methods: {
	
    iphoneConfig() {
      const isPro = process.env.NODE_ENV === "production";
      var u = navigator.userAgent;
      // var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      if (this.isFirst && isiOS) {
        this.isFirst = false;
        console.log("hash", this.$route.hash);
        let from = window.location.href.split("#")[1]
        if(from.indexOf('welcome')>-1){
          from = from.split('=')[1]
          
        }
        this.$router.push(
          `/welcome?from=${from}`
        );
      }

      // if (isiOS) {
      //     window.onpageshow = function(e) {
      //         if(e.persisted || (window.performance && window.performance.navigation.type == 2)) {
      //             window.location.reload()
      //         }
      //     }
      // }
    },
    getWeixinConfig() {
		console.log('getWeixinConfig')
      const _this = this;
      let params = {};
      _this.$request.getWeixinConfig(params).then((res) => {
		  console.log('res_getWeixinConfig',res)
        _this.$store.commit("setAppid", res.data.value.appid);
        _this.$store.commit("setSecret", res.data.value.secret);
        _this.$store.commit("setMininum", res.data.value.minimum); // 最小提现金额
        localStorage.setItem("MININUM", res.data.value.minimum);
        _this.getCode(res.data.value.appid)
        // if (process.env.NODE_ENV == "production") {
        //   _this.getCode(res.data.value.appid);
        // }
      });
    },
    getCode(appid) {
      // 非静默授权，第一次有弹框
      const _this = this;
      const code = this.getUrlParam("code"); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
      const local = window.location.href;
      if (code == null || code === "") {
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
          appid +
          "&redirect_uri=" +
          encodeURIComponent(local) +
          "&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect";
      } else {
        _this.$store.commit("setCode", code);
        _this.getOpenId(code); //把code传给后台获取用户信息
      }
    },
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    // getUserInfo(access_token, open_id) {
    //   // let getUserUrl = `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${open_id}⟨=zh_CN`;

    //   const _this = this;
    //   let params = {
    //       access_token: access_token,
    //       open_id: open_id
    //   }
    //   _this.$request.getUserInfo(params).then(res=>{
    //     alert("userInfo:"+JSON.stringify(res))
    //   })
    // },
    getFansItem(item) {
      console.log("当前用户的信息", item);
      const _this = this;
      let params = {
        openid: item.openid,
        nickname: item.nickname,
        name: item.nickname,
        avatar: item.avatar,
        province: item.province,
        city: item.city,
      };
      _this.$request.getFansItem(params).then((res) => {
        //默认
      });
    },
    getOpenId(code) {
      const _this = this;
      let params = {
        appid: _this.$store.state.appid,
        secret: _this.$store.state.secret,
        code: code,
      };
	  console.log("getOpenid",params)
      _this.$request.getOpenid(params).then((res) => {
        const userInfo = res.data[0];
        const openid = userInfo["openid"];
        const nickname = userInfo["nickname"];
        const headimgurl = userInfo["headimgurl"];
        const province = userInfo["province"];
        const city = userInfo["city"];
        _this.$store.commit("setOpenid", openid);
        //  alert("openid:"+openid);
        localStorage.setItem("OPENID", openid);
        const item = {
          openid,
          nickname,
          avatar: headimgurl,
          province,
          city,
          name: nickname,
        };
        _this.$store.commit("setUserInfo", item);
        localStorage.setItem("userInfo", JSON.stringify(item));
        _this.getFansItem(item);
		const merchant_id = localStorage.getItem('merchant_id')
		  if(!merchant_id){//若无，则去获取
				  this.getMerchantItem(openid)
		  }
		//this.getMerchantItem(openid);
        //获取推广员item
        // this.getPromoterItema(openid);
      });
    },
  },
};
</script>


<style lang="scss">
@import "@/assets/css/variable";
html {
  font-size: 10px !important;
  min-height: 100%;
}
@media only screen and (max-width: 374px) {
  /* iphone5 或者更小的尺寸，以 iphone5 的宽度（320px）比例设置 font-size */
  html {
    font-size: 10px !important;
  }
}
@media only screen and (min-width: 375px) and (max-width: 413px) {
  /* iphone6/7/8 和 iphone x */
  html {
    font-size: 11px !important;
  }
}
@media only screen and (min-width: 414px) {
  /* iphone6p 或者更大的尺寸，以 iphone6p 的宽度（414px）比例设置 font-size */
  html {
    font-size: 12px !important;
  }
}
body {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100%;
  box-sizing: border-box;
}
.ql-editor {
  min-height: 200px;
}
p {
  margin-bottom: 0 !important;
}
img {
  width: 100% !important;
}
.tip4help {
	padding-left: 9px;
	padding-top: 10px;
	color: red !important;
	line-height: 1.2em;
}
.load-more {
  text-align: center;
  background-color: white;
  // margin: 0 1rem;
  width: 94%;
  margin: 0 auto;
  box-sizing: border-box;
}
.no-data {
  text-align: center;
}
.d-none {
  display: none;
}
.padding-all {
  padding: 0.8rem;
}
.padding-all-m {
  padding: 1.4rem;
}
.padding-all-l {
  padding: 1.2rem;
}
.padding-lr {
  padding-left: 0.8rem;
  padding-right: 0.8rem;
}
.padding-lr-l {
  padding-left: 1.6rem;
  padding-right: 1.6rem;
}
.border-r-l {
  border-radius: 0.8rem;
}
.border-r-m {
  border-radius: 0.6rem;
}
.color-blue {
  color: blue;
}
.color-red {
  color: red;
}
.color-green {
  color: green;
}
.color-ll {
  color: #111;
}
.color-l {
  color: #333;
}
.color-m {
  color: #666;
}
.color-s {
  color: #999;
}
.margT-s {
  margin-top: 0.3rem;
}
.margT-m {
  margin-top: 0.4rem;
}
.margT-l {
  margin-top: 1rem;
}
.margL-m {
  margin-left: 0.6rem;
}
.margL-l {
  margin-left: 1rem;
}
.margB-l {
  margin-bottom: 1rem;
}
.t-center {
  text-align: center;
}
.t-bold {
  font-weight: bold;
}
.tabbar-height {
  height: 50px;
  width: 100%;
  background-color: $main-bgc;
}
.tabbar-height2 {
  height: 30px;
  width: 100%;
}
.line-height-m {
  line-height: 1.3em;
}
.line-height-l {
  line-height: 1.5em;
}
.main-bg {
  background-color: $main-bgc;
}
.title-l {
  font-size: 1.8rem;
  font-weight: bold;
  color: #111;
}
.title-m {
  font-size: 1.6rem;
  font-weight: bold;
  color: #222;
}
.title-s {
  font-size: 1.4rem;
  font-weight: bold;
  color: #333;
}
.fs-12 {
  font-size: 1.2rem;
}
.fs-13 {
  font-size: 1.3rem;
}
.fs-14 {
  font-size: 1.4rem;
}
.fs-15 {
  font-size: 1.5rem;
}
.fs-16 {
  font-size: 1.6rem;
}
.fs-18 {
  font-size: 1.8rem;
}

.base-info {
  background-color: white;
  width: 100%;
  box-sizing: border-box;
  .field-box {
    width: 100%;
    padding: 1.4rem 1.6rem;
    position: relative;
    box-sizing: border-box;
    &:after {
      position: absolute;
      box-sizing: border-box;
      content: " ";
      pointer-events: none;
      right: 16px;
      bottom: 0;
      left: 16px;
      border-bottom: 1px solid #ebedf0;
      -webkit-transform: scaleY(0.5);
      transform: scaleY(0.5);
    }
    .label {
      position: relative;
      font-size: 1.4rem;
      color: #646566;
      padding-bottom: 1.2rem;
      &:after {
        content: "*";
        color: red;
        position: absolute;
        left: -0.8rem;
      }
    }
    .my-img {
      min-height: 60px;
      width: 100%;
      text-align: center;
      box-sizing: border-box;
      position: relative;
      box-sizing: border-box;
      img {
        width: 100%;
        object-fit: fill;
      }
      .change-pic {
        position: absolute;
        width: 100%;
        height: 2.8rem;
        right: 0rem;
        bottom: 0.1rem;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: rgba($color: #000000, $alpha: 0.7);
        color: white;
        font-size: 1.2rem;
        box-sizing: border-box;
      }
    }
  }
}
.introduce {
  background-color: white;
  .introduce-title {
    font-size: 1.4rem;
  }
  .btnAddSomeBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .addSome {
      flex: 1;
    }
    .interval {
      flex: 0 0 1rem;
    }
  }
}
</style>
